﻿//Copyright (c) 2010, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace yaTDL.Core.MVVM
{
    public class MainPageViewModel : ViewModelBase
    {

        #region Fields

        bool _projectListViewIsVisible;
        bool _searchViewIsVisible;

        ProjectListViewModel _projectListViewModel = new ProjectListViewModel();

        SearchViewModel _searchViewModel = new SearchViewModel();

        RelayCommand _showSettingsCommand;
        RelayCommand _showSearchViewCommad;

        #endregion


        #region Properties

        public bool ProjectListViewIsVisible
        {
            get { return _projectListViewIsVisible; }
            set
            {
                if (value != _projectListViewIsVisible)
                {
                    _projectListViewIsVisible = value;
                    RaisePropertyChanged("ProjectListViewIsVisible");
                }
            }
        }

        public bool SearchViewIsVisible
        {
            get { return _searchViewIsVisible; }
            set
            {
                if (value != _searchViewIsVisible)
                {
                    _searchViewIsVisible = value;
                    RaisePropertyChanged("SearchViewIsVisible");
                }
            }
        }


        public ProjectListViewModel ProjectListViewModel
        {
            get { return _projectListViewModel; }
            set
            {
                if (value != _projectListViewModel)
                {
                    _projectListViewModel = value;
                    RaisePropertyChanged("ProjectListViewModel");
                }
            }
        }

        public SearchViewModel SearchViewModel
        {
            get
            {
                return _searchViewModel;
            }
            set
            {
                if (value != _searchViewModel)
                {
                    _searchViewModel = value;
                    RaisePropertyChanged("SearchViewModel");
                }
            }
        }        

        public ICommand ShowSettingsCommand
        {
            get
            {
                if (_showSettingsCommand == null)
                {
                    _showSettingsCommand = new RelayCommand(p => this.showSettingsCommandExecute(p));
                }
                return _showSettingsCommand;
            }
        }

        public ICommand ShowSearchViewCommand
        {
            get 
            {
                if (_showSearchViewCommad == null)
                {
                    _showSearchViewCommad = new RelayCommand(p => this.showSeachViewCommandExecute(p), p => this.showSearchViewCommandCanExecute(p));
                }
                return _showSearchViewCommad;
            }
        }

        #endregion


        #region Constructor(s) and Private Methods

        public MainPageViewModel()
        {
            ViewManager.RegisterView(this.showProjectListViewModel, typeof(ProjectListViewModel));

            ProjectListViewIsVisible = true;
        }




        private void showProjectListViewModel(ViewModelBase vm, Action<object> callback)
        {
            if (!Dispatcher.CurrentDispatcher.CheckAccess())
            {
                Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { showProjectListViewModel(vm, callback); }));
                return;
            }


            this.ProjectListViewModel = vm as ProjectListViewModel;
            ProjectListViewIsVisible = true;


        }


        #endregion


        #region Command Methods

        private void showSettingsCommandExecute(object parameter)
        {
            ViewManager.ShowView(new SettingsViewModel());
        }


        private bool showSearchViewCommandCanExecute(object parameter = null)
        {
            return true;
        }
        
        private void showSeachViewCommandExecute(object parameter = null)
        {
            SearchViewIsVisible = false;    //first set it to false for the case it is already shown. This way, the SeachBox get's focused when Crtl+F is pressed, even if the SearchView is already shown btut the search-box is not focused.
            SearchViewIsVisible = true;
        }


        #endregion
    }

}